package com.quanyan.place.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.ServletUtils;
import com.quanyan.place.entity.apireq.ReqMemberCardType;
import com.quanyan.place.entity.apiresp.RespPlaceMemberCardDetail;
import com.quanyan.place.wrapper.PlaceMemberWrapper;
import com.quanyan.place.wrapper.PlaceWrapper;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/10/28.
 */
@RestController
@RequestMapping("/api/place/member")
public class ApiPlaceMemberController {

    @Autowired
    PlaceWrapper placeWrapper;

    @Autowired
    PlaceMemberWrapper placeMemberWrapper;

    /**
     * 客户端场馆会员卡列表List
     * @param map
     * @return
     */
    @RequestMapping(value = "/getPlaceMemberCardTypeList")
    public APIResponse<?> getPlaceMemberCardTypeList(@RequestBody @Valid Map<String, Object> map){
        Integer placeId = MapUtils.getInteger(map, "venueId");
        if (null == placeId){
            APIResponse.returnFail("参数错误！");
        }
        return placeMemberWrapper.getPlaceMemberCardTypeList(placeId);
    }


    /**
     * 客户端购买会员卡
     * @param reqMemberCardType
     * @return
     */
    @RequestMapping(value = "/buyMemberCard", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> buyMemberCard(@RequestBody @Valid ReqMemberCardType reqMemberCardType, BindingResult bindingResult)  {
        //APIResponse apiResponse = placeMemberWrapper.insertMemberType(reqMemberCardType);
        return APIResponse.returnSuccess();
    }

    /**
     * 客户端会员卡详情
     * @param map
     * @return
     */
    @RequestMapping(value = "/placeMemberCardDetail")
    public APIResponse<?> placeMemberCardDetail(@RequestBody @Valid Map<String, Object> map){
        String innerCardNo = MapUtils.getString(map, "vipNo");
        if (null == innerCardNo){
            APIResponse.returnFail("参数错误！");
        }
        return placeMemberWrapper.getPlaceMemberCardDetail(innerCardNo);
    }

    /**
     * app会员卡类型详情
     * @param map
     * @return
     */
    @RequestMapping(value = "/placeMemberCardTypeDetail")
    public APIResponse<?> placeMemberCardTypeDetail(@RequestBody @Valid Map<String, Object> map){
        Integer cardTypeId = MapUtils.getInteger(map, "cardTypeId");
        Integer placeId = MapUtils.getInteger(map, "placeId");
        if (null == cardTypeId){
            APIResponse.returnFail("参数错误！");
        }
        return placeMemberWrapper.getPlaceMemberCardType(placeId, cardTypeId);
    }

    /**
     *  获取会员卡订单详情
     * @param map
     * @return
     */
    @RequestMapping(value = "/getVipPayDetail", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getVipPayDetail(@RequestBody Map<String, Object> map){
        Integer placeId = MapUtils.getInteger(map, "placeId");
        Integer cardTypeId = MapUtils.getInteger(map, "cardTypeId");

        if (StringUtils.isEmpty(placeId) || StringUtils.isEmpty(cardTypeId)){
            return APIResponse.returnFail("参数错误");
        }

        return placeMemberWrapper.getVipPayDetail(placeId, cardTypeId);
    }

    /**
     * 获取我的场馆会员卡信息列表
     * @return
     */
    @RequestMapping(value = "/getMyVenueVipList", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<List<RespPlaceMemberCardDetail>> getMyVenueVipList(){
        HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
        Integer userId = (Integer) httpServletRequest.getAttribute("uid");
//        userId = 3706;
        if (!StringUtils.isEmpty(userId) && userId != 0) {
            return placeMemberWrapper.getMyVenueVipList(userId);
        }
        return APIResponse.returnFail("用户没有登录");
    }

    /**
     * 购买会员卡-付费
     * @param map
     * @return
     */
    @RequestMapping(value = "/buyVenueVip", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> buyVenueVip(@RequestBody Map<String, Object> map){
        HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
        Integer userId = (Integer) httpServletRequest.getAttribute("uid");
//        userId = 3706;
        Integer cardTypeId = MapUtils.getInteger(map, "cardTypeId");
        Integer payTypeId = MapUtils.getInteger(map, "payTypeId");
        Integer platformType = MapUtils.getInteger(map, "platformType");
        Integer refereesId = MapUtils.getInteger(map,"refereesId");
        Integer larkAppId = MapUtils.getInteger(map, "larkAppId");
        if (StringUtils.isEmpty(cardTypeId)){
            return APIResponse.returnFail("会员卡类型id不能为空");
        }
        if (StringUtils.isEmpty(payTypeId)){
            return APIResponse.returnFail("没有选择支付方式");
        }
        if (StringUtils.isEmpty(larkAppId)) {
            larkAppId = 6;
        }
        if (!StringUtils.isEmpty(userId) && userId != 0) {
            return placeMemberWrapper.buyVenueVip(userId, cardTypeId, payTypeId, platformType,refereesId,larkAppId);
        }
        return APIResponse.returnFail("用户没有登录");
    }

    /**
     * 会员卡消费记录列表
     * @param map
     * @return
     */
    @RequestMapping(value = "/vipCardPayRecordList", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> vipCardPayRecordList(@RequestBody Map<String, Object> map){
        HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
        Integer userId = (Integer) httpServletRequest.getAttribute("uid");
//        userId = 3706;
        String vipNo = MapUtils.getString(map,"vipNo");
        Integer pageNum = MapUtils.getInteger(map, "pageNum");
        Integer pageSize = MapUtils.getInteger(map, "pageSize");
        if (StringUtils.isEmpty(vipNo)){
            return APIResponse.returnFail("会员卡号不能为空");
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum = Constants.DEFAULT_PAGE_NUM;
        }
        if (StringUtils.isEmpty(pageSize)){
            pageSize = Constants.DEFAULT_PAGE_SIZE;
        }
        if (!StringUtils.isEmpty(userId) && userId != 0) {
//            return placeMemberWrapper.vipCardPayRecordList(userId, pageNum, pageSize);
            return placeMemberWrapper.vipCardNoPayRecordList(vipNo, userId, pageNum, pageSize);
        }
        return APIResponse.returnFail("用户没有登录");
    }

    /**
     * 会员卡充值（再次购买）-订单详情页
     * @param map
     * @return
     */
    @RequestMapping(value = "/vipCardReChargeDetail", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> vipCardReChargeDetail(@RequestBody Map<String, Object> map){
        HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
        Integer userId = (Integer) httpServletRequest.getAttribute("uid");
//        userId = 3706;
        Integer cardTypeId = MapUtils.getInteger(map, "cardTypeId");
        Integer payTypeId = MapUtils.getInteger(map, "payTypeId");
        Integer platformType = MapUtils.getInteger(map, "platformType");
        Integer refereesId = MapUtils.getInteger(map,"refereesId");
        if (StringUtils.isEmpty(cardTypeId)){
            return APIResponse.returnFail("会员卡类型id不能为空");
        }
        if (StringUtils.isEmpty(payTypeId)){
            return APIResponse.returnFail("没有选择支付方式");
        }

        if (!StringUtils.isEmpty(userId) && userId != 0) {
            return placeMemberWrapper.vipCardReChargeDetail(userId, cardTypeId, payTypeId,  platformType,refereesId);
        }
        return APIResponse.returnFail("用户没有登录");
    }
}
